package com.fy56.platform.logistics.redis.dao;

import com.fy56.platform.logistics.redis.dbutil.RedisUtilsTemplate;
import com.fy56.platform.logistics.redis.dbutil.TokenConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

/**
 * Redis DAO层基础
 * Created by li on 2018-02-05 13:54
 */
@Repository
public abstract class AbstractBaseRedisDao<K, V> {

    @Autowired
    @Qualifier("redisUtils")
    protected RedisUtilsTemplate redisUtils;

    public Object get(String key){
        return redisUtils.get(key);
    }

    public void set(String key, String val){
        redisUtils.set(String.valueOf(key),val, TokenConstants.TOKEN_EXPIRES_SECONDS, TimeUnit.SECONDS);
    }

    public void delete(String key){  redisUtils.del(key); }

    /**
     * 添加缓存
     * @param key 键
     * @param val 值
     * @param overTime 失效时间(单位:秒), 默认:360秒
     */
    public void set(String key, String val, int overTime){
        if(overTime<=0){
            overTime = TokenConstants.TOKEN_EXPIRES_SECONDS;
        }
        redisUtils.set(String.valueOf(key),val, overTime, TimeUnit.SECONDS);
    }

    /**
     * 指定缓存失效时间
     * @param key 键
     * @param overTime 失效时间(单位:秒), 默认:360秒
     */
    public void expire(String key, int overTime){
        if(overTime<=0){
            overTime = TokenConstants.TOKEN_EXPIRES_SECONDS;
        }
        redisUtils.expire(key, overTime, TimeUnit.SECONDS);
    }

    /**
     * 判断key是否存在
     * @param key 键
     * @return [true存在, false不存在]
     */
    public boolean hasKey(String key){ return redisUtils.hasKey(key); }
}
